package com.stylefeng.guns.modular.computer.controller;

import com.stylefeng.guns.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.node.ZTreeNode;
import com.stylefeng.guns.common.persistence.dao.ComputerMapper;
import com.stylefeng.guns.common.persistence.dao.ComputerTypeMapper;
import com.stylefeng.guns.common.persistence.model.Computer;
import com.stylefeng.guns.common.persistence.model.ComputerType;
import com.stylefeng.guns.common.redis.RedisUtils;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.computer.dao.ComputerDao;
import com.stylefeng.guns.modular.computer.service.IComputerService;
import com.stylefeng.guns.modular.computertype.service.IComputertypeService;
import com.stylefeng.guns.modular.system.warpper.ComputerWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * computer控制器
 *
 * @author fengshuonan
 * @Date 2019-07-10 16:57:43
 */
@Controller
@RequestMapping("/computer")
public class ComputerController extends BaseController {

    private String PREFIX = "/computer/computer/";
    @Resource
    private ComputerMapper computerMapper;
    @Resource
    private ComputerTypeMapper computerTypeMapper;
    @Resource
    private IComputerService computerService;
    @Autowired
    private RedisUtils redisUtils;
    @Resource
    private ComputerDao computerDao;

    /**
     * 跳转到computer首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "computer.html";
    }

    /**
     * 跳转到添加computer
     */
    @RequestMapping("/computer_add")
    public String computerAdd(Model model) {
        List<ComputerType> conputerType=computerService.search(null);
        model.addAttribute("conputerType",conputerType);
        return PREFIX + "computer_add.html";
    }

    /**
     * 跳转到修改computer
     */
    @RequestMapping("/computer_update/{computerId}")
    public String computerUpdate(@PathVariable Integer computerId, Model model){
        Computer computer = this.computerMapper.selectById(computerId);
        model.addAttribute("computer", computer);
        List<ComputerType> computerTypeList=computerService.search(null);
        model.addAttribute("computerTypeList",computerTypeList);
        ComputerType ctype = computerTypeMapper.selectById(computer.getPid());
        model.addAttribute("ctype", ctype);
        LogObjectHolder.me().set(computer);

        return PREFIX + "computer_edit.html";
    }

    /**
     * 获取部门的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.computerDao.tree();
        return tree;
    }
    /**
     * 获取computer列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> computer = this.computerService.selectComputer(condition);
        Map<String, Object> list=computer.get(1);
        redisUtils.set("list",list);
        return super.warpObject(new ComputerWarpper(computer));
    }

    /**
     * 新增computer
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Computer computer) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM--dd");
        computer.setCreatetime(simpleDateFormat.format(new Date()));
        return this.computerService.addComputer(computer);

    }

    /**
     * 删除computer
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer computerId) {
        computerMapper.deleteById(computerId);
        return SUCCESS_TIP;
    }


    /**
     * 修改computer
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Computer computer)  {
        this.computerService.updateComputer(computer);
       //this.computerMapper.updateById(computer);
        return super.SUCCESS_TIP;
    }

    /**
     * computer详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {

        return null;
    }
}
